package com.swz.dcrm.ui.home;


import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.R;
import com.swz.dcrm.adpter.CustomAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.adpter.TodayPlanAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.ClueRecord;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.ui.viewmodel.TodayPlanViewModel;
import com.swz.dcrm.util.Constant;
import com.swz.dcrm.util.CustomDecoration;
import com.swz.dcrm.util.RxTimer;
import com.swz.dcrm.wrapper.EmptyWrapper;
import com.xh.baselibrary.callback.OnClickListener;


import javax.inject.Inject;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class TodayPlanItemFragment extends BaseFragment {


    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private int page = 1;
    private int pageSize = 15;
    private int total;
    private int status;


    private TodayPlanAdapter todayPlanAdapter;

    private EmptyWrapper emptyWrapper;

    RxTimer rxTimer;


    @Inject
    TodayPlanViewModel todayPlanViewModel;
    @Inject
    MainViewModel mainViewModel;

    public static TodayPlanItemFragment newInstance(String s, int status) {
        TodayPlanItemFragment fragment = new TodayPlanItemFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", s);
        bundle.putInt("status", status);
        fragment.setArguments(bundle);
        return fragment;
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        Bundle bundle = getArguments();
        status = bundle.getInt("status");
        String type = bundle.getString("type");
        switch (type) {
            case Constant.TODAY_ARRIVE:
            case Constant.TODAY_BOOKING:
            case Constant.TODAY_CONTACT:

                todayPlanViewModel.getTodayContact(type, status, page, pageSize)
                        .observe(this, observer);
                break;
        }
    }

    Observer<BaseResponse<Page<ClueRecord>>> observer=new Observer<BaseResponse<Page<ClueRecord>>>() {
        @Override
        public void onChanged(BaseResponse<Page<ClueRecord>> pageBaseResponse) {
            if(pageBaseResponse==null){
                return;
            }
            mHolder.showLoadSuccess();

            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.finishLoadmore();
            if (pageBaseResponse.isSuccess()) {
                if (todayPlanAdapter == null) {
                    total = pageBaseResponse.getData().getTotal();
                    todayPlanAdapter = new TodayPlanAdapter(getContext(), pageBaseResponse.getData().getList(), R.layout.item_today_plan, status);
                    todayPlanAdapter.setOnClickListener(onClickListener);
                    todayPlanAdapter.setOnButtonClick(onButtonClick);
                    emptyWrapper=getEmptyWrapper(todayPlanAdapter,R.mipmap.empty_task);
                    rv.setAdapter(emptyWrapper);
                } else {
                    todayPlanAdapter.refresh(pageBaseResponse.getData().getPageNum(), pageBaseResponse.getData().getList());
                    emptyWrapper.notifyDataSetChanged();
                }
            }
        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(rxTimer.isDisposable()){
            page=1;
            onLoadRetry();
            smartRefreshLayout.setEnableLoadmore(true);
        }
    }



    @Override
    public int layoutId() {
        return R.layout.fragment_today_plan_item;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.addItemDecoration(new CustomDecoration(getContext(), 0, 10, 10, 10));
        smartRefreshLayout.setOnRefreshListener(refreshlayout -> {
            page=1;
            onLoadRetry();
            smartRefreshLayout.setEnableLoadmore(true);
        });
        smartRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
            if (todayPlanAdapter != null && todayPlanAdapter.getItemCount() >= total) {

                smartRefreshLayout.finishLoadmore();
                smartRefreshLayout.setEnableLoadmore(false);
                return;
            } else {
                page++;
                onLoadRetry();
            }
        });
        mHolder.showLoadingStatus(GlobalAdapter.LOADING_NOT_NEED_MARGIN);
        rxTimer=new RxTimer();
        rxTimer.timer(400,number -> {
            onLoadRetry();
        });

        return false;
    }

    private OnClickListener<ClueRecord> onClickListener = (clueRecord) -> {
        ClueDetailFragmentArgs.Builder builder = new ClueDetailFragmentArgs.Builder();
        builder.setClueId(new Gson().toJson(clueRecord.getBsClueVO().getId()));
        go(null, R.id.action_todayContactFragment_to_clueDetailFragment, builder.build().toBundle());
    };
    private TodayPlanAdapter.OnButtonClick onButtonClick = new TodayPlanAdapter.OnButtonClick() {

        @Override
        public void onLogging(ClueRecord clueRecord) {
            AddVisitRecordFragmentArgs.Builder builder = new AddVisitRecordFragmentArgs.Builder();
            builder.setClueRecord(new Gson().toJson(clueRecord));
            go(null, R.id.action_todayContactFragment_to_addVisitRecordFragment, builder.build().toBundle());
        }

        @Override
        public void onCall(String phone) {
            mainViewModel.getCall().setValue(phone);
        }

        @Override
        public void onHandUp(long clueId) {
            ReasonFragmentArgs.Builder builder=new ReasonFragmentArgs.Builder();
            builder.setTitle(getString(R.string.clue_detail_hand_up));
            builder.setClueId(String.valueOf(clueId));
            builder.setTag(getString(R.string.clue_detail_reason,builder.getTitle()));
            go(null,R.id.action_todayContactFragment_to_reasonFragment,builder.build().toBundle());
        }
    };


}
